//
//  HWTabBarViewController.m
//  黑马微博2期
//
//  Created by apple on 14-10-7.
//  Copyright (c) 2014年 heima. All rights reserved.
//
#import "PlayViewController.h"
#import "RadioListCollectionViewController.h"
#import "MFSideMenu.h"
#import "LoveManage.h"
#import "Reachability.h"
#import "NetworkChange.h"
#import "PlayManage.h"



#import "XFEssenceViewController.h"
#import "XFLatestViewController.h"
#import "HWComposeViewController.h"
#import "FXBlurView.h"
#import "HWTabBarViewController.h"
#import "HWConcernViewController.h"
#import "HWEssenceViewController.h"
#import "HWHomeViewController.h"
#import "HWMeViewController.h"
#import "HWNavigationController.h"
#import "HWTabBar.h"

@interface HWTabBarViewController ()<HWTabBarDelegate>
@property (nonatomic,weak)UIButton *plus;
@property (nonatomic,weak)FXBlurView *blurView;
@property (nonatomic,weak)UIImageView *text;
@property (nonatomic,weak)UIImageView *ablum;
@property (nonatomic,weak)UIImageView *camera;
@property (nonatomic,weak)UIImageView *sign;
@property (nonatomic,weak)UIImageView *comment;
@property (nonatomic,weak)UIImageView *more;

@end

@implementation HWTabBarViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initialControllers];
    
    //替换系统tabbar
    [self setValue:[[HWTabBar alloc] init] forKeyPath:@"tabBar"];
      
}

- (void)didReceiveMemoryWarning{

    [super didReceiveMemoryWarning];
}

//初始化子控制器
-(void)initialControllers {
    
    [self setupController:[[HWEssenceViewController alloc]init] image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon" title:@"精华"];
    [self setupController:[[XFEssenceViewController alloc]init] image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon" title:@"最新"];
    
    //[self setupController:[[HWHomeViewController alloc]init] image:@"tabBar_friendTrends_click_icon" selectedImage:@"tabBar_friendTrends_click_icon"title:@"关注"];
    [self setupController:[[HWConcernViewController alloc]init] image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon" title:@"订阅"];
    [self setupController:[[HWMeViewController alloc]init] image:@"tabBar_me_icon"  selectedImage:@"tabBar_me_click_icon" title:@"我"];
    
    
}

//设置控制器
-(void)setupController:(UIViewController *)childVc image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title {
    
    UIViewController *viewVc = childVc;
    viewVc.tabBarItem.image = [UIImage imageNamed:image];
    viewVc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    viewVc.tabBarItem.title = title;
    HWNavigationController *navi = [[HWNavigationController alloc]initWithRootViewController:viewVc];
    [self addChildViewController:navi];
    
}

#pragma mark - HWTabBarDelegate代理方法
-(void)tabBarDidClickPlusButton:(HWTabBar *)tabBar {
    
    FXBlurView *blurView = [[FXBlurView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    blurView.tintColor = [UIColor clearColor];
    self.blurView = blurView;
    
    [self.view addSubview:blurView];
    
    UIImageView *compose = [[UIImageView alloc]init];
    [compose setImage:[UIImage imageNamed:@"compose_slogan"]];
    compose.frame = CGRectMake(0, 100, self.view.frame.size.width, 48);
    compose.contentMode = UIViewContentModeCenter;
    [blurView addSubview:compose];
    
    
    UIView *bottom = [[UIView alloc]init];
    
    bottom.frame = CGRectMake(0, self.view.bounds.size.height - 44, self.view.bounds.size.height, 44);
    
    bottom.backgroundColor = [UIColor whiteColor];
    
    //bottom.contentMode = UIViewContentModeCenter;
    
    
    UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
    
    plus.frame = CGRectMake((self.view.bounds.size.width - 25) * 0.5, 8, 25, 25);
    
    [plus setImage:[UIImage imageNamed:@"tabbar_compose_background_icon_add"] forState:UIControlStateNormal];
    
    [bottom addSubview:plus];
    
    [UIView animateWithDuration:0.2 animations:^{
        
        plus.transform = CGAffineTransformMakeRotation(M_PI_4);
        self.plus = plus;
        
    }];
    
    [plus addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    [blurView addSubview:bottom];
    
    
    UIImageView *text = [self btnAnimateWithFrame:CGRectMake((SCREEN_WIDTH-213)*1/4.0 + 71 *0, 500, 71, 71) imageName:@"tabbar_compose_idea" text:@"微博" animateFrame:CGRectMake((SCREEN_WIDTH-213)*1/4.0 + 71 *0, 280, 71, 71) delay:0.0];
    text.layer.masksToBounds = YES;
    text.layer.cornerRadius = 35.5;
    [self setAction:text action:@selector(compose)];
    self.text = text;
    
    UIImageView *ablum = [self btnAnimateWithFrame:CGRectMake((SCREEN_WIDTH-213)*2/4.0 + 71 *1, 500, 71, 71) imageName:@"tabbar_compose_photo" text:@"音乐" animateFrame:CGRectMake((SCREEN_WIDTH-213)*2/4.0 + 71 *1, 280, 71, 71) delay:0.1];
    ablum.layer.masksToBounds = YES;
    ablum.layer.cornerRadius = 35.5;
    [self setAction:ablum action:@selector(radio)];
    self.ablum = ablum;

    
    UIImageView *camera = [self btnAnimateWithFrame:CGRectMake((SCREEN_WIDTH-213)*3/4.0 + 71 *2, 500, 71, 100) imageName:@"tabbar_compose_camera" text:@"摄影" animateFrame:CGRectMake((SCREEN_WIDTH-213)*3/4.0 + 71 *2, 280, 71, 100) delay:0.15];
    self.camera = camera;
    
    UIImageView *sign = [self btnAnimateWithFrame:CGRectMake((SCREEN_WIDTH-213)*1/4.0 + 71 *0, 700, 71, 100) imageName:@"tabbar_compose_lbs" text:@"签到" animateFrame:CGRectMake((SCREEN_WIDTH-213)*1/4.0 + 71 *0, 410, 71, 100) delay:0.2];
    self.sign = sign;
    
    
    UIImageView *comment = [self btnAnimateWithFrame:CGRectMake((SCREEN_WIDTH-213)*2/4.0 + 71 *1, 700, 71, 100) imageName:@"tabbar_compose_review" text:@"评论" animateFrame:CGRectMake((SCREEN_WIDTH-213)*2/4.0 + 71 *1, 410, 71, 100) delay:0.25];
    self.comment = comment;
    
    UIImageView *more = [self btnAnimateWithFrame:CGRectMake((SCREEN_WIDTH-213)*3/4.0 + 71 *2, 700, 71, 100) imageName:@"tabbar_compose_more" text:@"更多" animateFrame:CGRectMake((SCREEN_WIDTH-213)*3/4.0 + 71 *2, 410, 71, 100) delay:0.3];
    self.more = more;
    
    
}

//按钮出来动画
-(UIImageView *)btnAnimateWithFrame:(CGRect)frame imageName:(NSString *)imageName text:(NSString *)text animateFrame:(CGRect)aniFrame delay:(CGFloat)delay {
    
    UIImageView *btnContainer = [[UIImageView alloc]init];
    
    btnContainer.frame  = frame;
    
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
    
    [btnContainer addSubview:image];
    
    UILabel *word = [[UILabel alloc]initWithFrame:CGRectMake(0, 75, 71, 25)];
    [word setText:text];
    [word setTextAlignment:NSTextAlignmentCenter];
    [word setFont:[UIFont systemFontOfSize:15]];
    [word setTextColor:[UIColor grayColor]];
    
    [btnContainer addSubview:word];
    
    [self.blurView addSubview:btnContainer];
    
    [UIView animateWithDuration:0.5 delay:delay usingSpringWithDamping:0.6 initialSpringVelocity:0.05 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        
        btnContainer.frame  = aniFrame;
        
    } completion:^(BOOL finished) {
        
    }];
    
    return btnContainer;
}


//设置按钮方法
-(void)setAction:(UIImageView *)imageView action:(SEL)action{
    
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:action];
    imageView.userInteractionEnabled = YES;
    [imageView addGestureRecognizer:gesture];
    
}


//发文字微博
-(void)compose {
    
    [self closeClick];
    
    //    HWComposeViewController *compose = [[HWComposeViewController alloc]init];
    //
    //    XFNavigationController *nav = [[XFNavigationController alloc]initWithRootViewController:compose];
    //
    //    [self presentViewController:nav animated:YES completion:nil];
    
    HWHomeViewController *compose = [[HWHomeViewController alloc]init];
    
    HWNavigationController *nav = [[HWNavigationController alloc]initWithRootViewController:compose];
    
    [self presentViewController:nav animated:YES completion:nil];
}


//发文字微博
-(void)radio {
    
    [self closeClick];
    //radio List
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = [UIScreen mainScreen].bounds.size;
    RadioListCollectionViewController *radioCC = [[RadioListCollectionViewController alloc] initWithCollectionViewLayout:flowLayout];
    //play View
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"PlayViewController" bundle:nil];
    PlayViewController *playVC = [storyboard instantiateViewControllerWithIdentifier:@"playVC"];
    
    MFSideMenuContainerViewController *sideMenu = [MFSideMenuContainerViewController containerWithCenterViewController:playVC leftMenuViewController:radioCC rightMenuViewController:nil];
    sideMenu.leftMenuWidth = [[UIScreen mainScreen] bounds].size.width * 3.0 / 4.0;
    
    
    //添加网络监控
    [[NSNotificationCenter defaultCenter] addObserver:[NetworkChange share] selector:@selector(reachChange:) name:kReachabilityChangedNotification object:NO];
    
    [[[NetworkChange share] reachability] startNotifier];
    
    
    [self presentViewController:sideMenu animated:YES completion:nil];
    
    
    
    
}


//关闭动画
-(void)btnCloseAnimateWithFrame:(CGRect)rect delay:(CGFloat)delay btnView:(UIImageView *)btnView{
    
    
    [UIView animateWithDuration:0.3 delay:delay usingSpringWithDamping:0.6 initialSpringVelocity:0.05 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        
        btnView.frame  = rect;
        
    } completion:^(BOOL finished) {
        
    }];
    
}



//关闭按钮
-(void)closeClick {
    
    [UIView animateWithDuration:0.6 animations:^{
        
        self.plus.transform = CGAffineTransformMakeRotation(-M_PI_2);
        [self btnCloseAnimateWithFrame:CGRectMake(273, 700, 71, 100) delay:0.1 btnView:self.more];
        [self btnCloseAnimateWithFrame:CGRectMake(152, 700, 71, 100) delay:0.15 btnView:self.comment];
        [self btnCloseAnimateWithFrame:CGRectMake(31, 700, 71, 100) delay:0.2 btnView:self.sign];
        [self btnCloseAnimateWithFrame:CGRectMake(273, 700, 71, 100) delay:0.25 btnView:self.camera];
        [self btnCloseAnimateWithFrame:CGRectMake(152, 700, 71, 100) delay:0.3 btnView:self.ablum];
        [self btnCloseAnimateWithFrame:CGRectMake(31, 700, 71, 100) delay:0.35 btnView:self.text];
        
        
    } completion:^(BOOL finished) {
        
        [self.text removeFromSuperview];
        [self.ablum removeFromSuperview];
        [self.camera removeFromSuperview];
        [self.sign removeFromSuperview];
        [self.comment removeFromSuperview];
        [self.more removeFromSuperview];
        [self.blurView removeFromSuperview];
    }];
    
    
}


@end
